﻿#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "spdlog/spdlog.h"
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "./huaweiSdk/utils/stringUtils.h"
#include "./huaweiSignSdk/RequestParams.h"
#include "./huaweiSignSdk/signer.h"
#include "./utils/ipUtils.h"
#include "./utils/commandUtils.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "httplib.h"
#include "./huaweiSdk/IAM.h"
#include "./huaweiSdk/DNS.h"


//一天毫秒数
#define DAY_MILLISECOND 86400000

using namespace std;
using namespace httplib;

// 上一次更新IP与更新token的时间
//ULONGLONG lastTokenTime = 0;
//string token;
ULONGLONG lastIpTime = 0;
string ip;

// 用来签名的ak sk
string ak;
string sk;

// 要解析到的域名对应的名字,域名id和记录集id
string recordset_name;
string recordset_type;
string zone_id;
string recordset_id;

// 登录信息
string domain_name;
string IAM_name;
string password;

// ipconfig的适配器和ip对应的字符串
string adapter_string;
string ip_name_string;

// 统一身份认证对象
IAM* iam;

DNS* dns;

// 创建一个日志记录器
shared_ptr<spdlog::logger> logger = spdlog::basic_logger_mt("logger", "logs/logtest.txt");

// 获取ip并且更新解析
static bool flushIp();

// 初始化函数，初始化写在配置文件里面的参数
static void init();

int main() {
    // 设置日志在trace级别以上时立即刷新
    logger->flush_on(spdlog::level::trace);
    // 设置日志级别
    logger->set_level(spdlog::level::info);
    init();
    while (1)
    {
        flushIp();
        Sleep(5000);
    }
}

static void init()
{
    // 设置locale为支持UTF-8
    // std::locale::global(std::locale("en_US.UTF-8"));

    // 创建一个读取配置文件的对象
    boost::property_tree::ptree pt;

    // 从INI文件读取
    boost::property_tree::ini_parser::read_ini("config.ini", pt);

    // 获取ak sk
    ak = pt.get<std::string>("dns.ak");
    sk = pt.get<std::string>("dns.sk");

    // 获取zone_id和recordset_id
    recordset_name = pt.get<std::string>("dns.recordset_name");
    recordset_type = pt.get<std::string>("dns.recordset_type");
    zone_id = pt.get<std::string>("dns.zone_id");
    recordset_id = pt.get<std::string>("dns.recordset_id");

    // 获取登录信息
    domain_name = pt.get<std::string>("login.domain_name");
    IAM_name = pt.get<std::string>("login.IAM_name");
    password = pt.get<std::string>("login.password");

    adapter_string = pt.get<std::string>("ipconfig.adapter_string");
    ip_name_string = pt.get<std::string>("ipconfig.ip_name_string");

    iam = new IAM(ak, sk, &logger);
    iam->setLoginInfo(domain_name, IAM_name, password);

    dns = new DNS(iam);
    return;
}
// 获取ip并且更新解析
static bool flushIp()
{
    // 先获取ip
    string tempIp = getlocalip(adapter_string, ip_name_string);

    // 获取不到ip
    if (tempIp == "")
    {
        logger->info("获取ip失败，请检查网络连接");
        return false;
    }
    
    // 不需要更新解析
    if (ip == tempIp) return true;


    logger->info("获取到新ip地址： " + tempIp);

    //更新ip

    // 创建recordset对象
    RecordSet recordSet(recordset_name, recordset_type, zone_id, recordset_id, "auto update recordset", 60, tempIp, 1);
    

    // 根据recordset对象调用api刷新解析
    bool success = dns->updateRecordset(recordSet);
    if (success)
    {
        logger->info((string)"成功更新域名：" + recordset_name + "解析到" + tempIp);
        ip = tempIp;
        lastIpTime = GetTickCount64();
    }
    else logger->info("更新解析失败");

    return success;
}
